const {
  Sequelize,
  Model
} = require('sequelize')
const { includes, clone, unset } = require('lodash')
const {
  sequelize
} = require('@root/core/db')
const {
  Art
} = require('@models/art')
const { User } = require('./user')

class Blog extends Model {
  /**
   * 查询当前登录用户的所有文章
   * @static
   * @param {*} author
   * @param {*} keyword
   * @memberof Blog
   */
  static async fetchAll(author, keyword) {
    const blogs = await Blog.findAll({
      order: [
        ['id', 'DESC']
      ]
    })
    if (!blogs) {
      throw new global.errors.NotFound()
    }
    let idList = []
    for(let obj of blogs) {
      if(!includes(idList, obj.user_id)) {
        idList.push(obj.user_id)
      }
    }
    if(idList.length === 0) {
      return []
    }
    const userInfoObj = await Art.userInfoList(idList)
    let fullObj = []
    blogs.forEach((item) => {
      fullObj.push({
        id: item.getDataValue('id'),
        title: item.getDataValue('title'),
        content: item.getDataValue('content'),
        created_at: item.getDataValue('created_at'),
        updated_at: item.getDataValue('updated_at'),
        author: {
          id: userInfoObj[item.getDataValue('user_id')].id,
          nickname: userInfoObj[item.getDataValue('user_id')].nickname
        },
      })
    })
    return fullObj
  }

  /**
   * 通过文章 id 获取文章详情
   * @static
   * @param {*} id
   * @memberof Blog
   */
  static async getBlogDetail(id) {
    const blog = await Blog.findOne({
      where: {
        id
      }
    })
    if(!id) {
      throw new global.errors.NotFound()
    }
    const user = await Art.userInfo(blog.getDataValue('user_id'))
    return {
      id: blog.getDataValue('id'),
      title: blog.getDataValue('title'),
      content: blog.getDataValue('content'),
      created_at: blog.getDataValue('created_at'),
      updated_at: blog.getDataValue('updated_at'),
      author: {
        id: user.getDataValue('id'),
        nickname: user.getDataValue('nickname')
      }
    }
  }

  /**
   * 更新文章
   * @static
   * @param {*} id
   * @param {*} [content={}]
   * @memberof Blog
   */
  static async updateBlog(id, title='', content={}) {
    const blog = await Blog.update({
      title,
      content
    },{
      where: {
        id
      }
    })
    return blog
  }

  /**
   * 添加文章
   * @static
   * @param {*} [content={}]
   * @memberof Blog
   */
  static async addBlog(userId, title='',content={}) {
    const blog = await Blog.create({
      title,
      content,
      user_id: userId
    })

    return blog
  }

  /**
   * 通过文章 id 删除文章
   * @static
   * @param {*} [content={}]
   * @memberof Blog
   */
  static async delBlog(id, userId) {
    const blog = await Blog.findOne({
      where: {
        id
      }
    })
    if(!blog) {
      throw new global.errors.NotFound()
    }
    const uid = blog.getDataValue('user_id')
    if(uid != userId) {
      throw new global.errors.NotFound('无法删除不是自己的文章')
    }
    return await blog.destroy({
      force: false
    })
  }
}

Blog.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: Sequelize.STRING(50),
    allowNull: false
  },
  content: {
    type: Sequelize.TEXT,
    allowNull: false
  }
}, {
  sequelize,
  tableName: 'blog'
})

User.hasMany(Blog, {
  foreignKey: 'user_id'
})

module.exports = {
  Blog
}
